package china.accountinghelper.activity;

import java.util.List;
import java.util.Map;

import com.xdroid.base.AsyncTaskBase;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import china.wc.tallyhelper.R;
import china.wc.tallyhelper.business.BusinessBook;
import china.wc.tallyhelper.business.BusinessExpense;
import china.wc.tallyhelper.controls.PieChartView;
import china.wc.tallyhelper.controls.SlideMenu;
import china.wc.tallyhelper.controls.SlideMenuItem;
import china.wc.tallyhelper.model.ModelBook;
import china.wc.tallyhelper.model.ModelPerson;
import china.wc.tallyhelper.model.ModelStatistics;

public class ActivityStatisticsBookDetail extends ActivityFrame
{
    private BusinessBook mBusinessBook;
    private BusinessExpense mBusinessExpense;

    private LinearLayout mFramebodyStatisticsBookDetail_LinearLayout_PieChart;
    private LinearLayout mFramebodyStatisticsBookDetail_LinearLayout_Statistics;
    
    private ModelBook mHandlingModelBook;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // 载入主页面Layout文件
        loadFramebody(R.layout.framebody_statistics_bookdetail);
        
        executeInitTask(null);
        
    }

    @Override
    public void onSlideMenuItemClick(SlideMenu pSlideMenu, SlideMenuItem pSlideMenuItem, AdapterView<?> pParent, View pView, int pPosition, long pId)
    {
        
    }

    @Override
    protected void initFields()
    {
        mBusinessBook = new BusinessBook();
        mBusinessExpense = new BusinessExpense();
        mHandlingModelBook = (ModelBook) getIntent().getSerializableExtra(ActivityStatistics.SELECTED_MODEL_OBJECT);
    }

    @Override
    protected void initViews()
    {
        mFramebodyStatisticsBookDetail_LinearLayout_PieChart = (LinearLayout) findViewById(R.id.FramebodyStatisticsBookDetail_LinearLayout_PieChart);
        mFramebodyStatisticsBookDetail_LinearLayout_Statistics = (LinearLayout) findViewById(R.id.FramebodyStatisticsBookDetail_LinearLayout_Statistics);
        
        LinearLayout.LayoutParams _LayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
        Map<String, Double> _Statistics = mBusinessExpense.statistics(mHandlingModelBook);
        
        PieChartView _PieChart = new PieChartView(this, _Statistics);
        _PieChart.setLayoutParams(_LayoutParams);
        
        mFramebodyStatisticsBookDetail_LinearLayout_PieChart.addView(_PieChart);
        
        List<ModelStatistics> _StatisticsList = mBusinessBook.statistics(mHandlingModelBook);
        if (_StatisticsList.size()<=0) {
            TextView _TextView = new TextView(this);
            _TextView.setLayoutParams(_LayoutParams);
            _TextView.setText(R.string.message_expense_noexpense);
            mFramebodyStatisticsBookDetail_LinearLayout_Statistics.addView(_TextView);
        } else {
            for (ModelStatistics _TemStatistics : _StatisticsList) {
                TextView _TextView = new TextView(this);
                _TextView.setLayoutParams(_LayoutParams);
                _TextView.setText(getString(R.string.message_statistics_detail, _TemStatistics.getConsumer().getName(),_TemStatistics.getPayer().getName(),_TemStatistics.getMoney2()));
                mFramebodyStatisticsBookDetail_LinearLayout_Statistics.addView(_TextView);
            }
        }
        
    }

    @Override
    protected void initListeners()
    {

    }

    @Override
    protected void bindData()
    {

    }

    @Override
    protected void register()
    {

    }
    
    @Override
    protected Object doInBackground(Object... params)
    {
        initFields();
        return null;
    }

    @Override
    protected void onPostExecute(Object result)
    {
        super.onPostExecute(result);
        initViews();
        initListeners();
        bindData();
        register();
     // 设置标题
        setTopTitle(getString(R.string.title_book_statistics, mHandlingModelBook.getName()));
    }

}
